//
//  AppDelegate.swift
//  customNavigationBarBackButton
//
//  Created by 李煜 on 2024/1/18.
// https://zhuanlan.zhihu.com/p/645330101

import UIKit

@main
class AppDelegate: UIResponder, UIApplicationDelegate {
    var window: UIWindow?

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        window = UIWindow(frame: UIScreen.main.bounds)
        window?.backgroundColor = .white
        window?.rootViewController = CustomNavigationController(rootViewController: AViewController())
        window?.makeKeyAndVisible()

        // https://zhuanlan.zhihu.com/p/645330101

        // 全局设置navigationbar backbutton
        let backImageBack = UIImage(named: "back")
        let backImageMask = UIImage(named: "backMask")
        let backImageCircle = UIImage(named: "backCircle")
        let backImageCurve = UIImage(named: "backCurve")

        if #available(iOS 15, *) {
            let appearance0 = UINavigationBarAppearance()
            appearance0.configureWithDefaultBackground()
            appearance0.setBackIndicatorImage(backImageBack, transitionMaskImage: backImageBack)
            // iOS 15  紧凑型,且有scrollview并且scrollview滚动到导航栏顶部时候的外观
            UINavigationBar.appearance().compactScrollEdgeAppearance = appearance0

            let appearance_standard = UINavigationBarAppearance()
            appearance_standard.configureWithDefaultBackground()
            appearance_standard.setBackIndicatorImage(backImageMask, transitionMaskImage: backImageMask)

            let appearance_compact = UINavigationBarAppearance()
            appearance_compact.configureWithDefaultBackground()
            appearance_compact.setBackIndicatorImage(backImageCircle, transitionMaskImage: backImageCircle)

            let appearance_scroll = UINavigationBarAppearance()
            appearance_scroll.configureWithDefaultBackground()
            appearance_scroll.setBackIndicatorImage(backImageCurve, transitionMaskImage: backImageCurve)

            // 默认导航栏外观
            UINavigationBar.appearance().standardAppearance = appearance_standard
            // 紧凑型导航栏外观
            UINavigationBar.appearance().compactAppearance = appearance_compact
            // UIScrollView滚动到导航栏顶部的时候外观
            UINavigationBar.appearance().scrollEdgeAppearance = appearance_scroll
        } else if #available(iOS 13, *) {
            let appearance1 = UINavigationBarAppearance()
            appearance1.configureWithDefaultBackground()
            appearance1.setBackIndicatorImage(backImageMask, transitionMaskImage: backImageMask)

            let appearance2 = UINavigationBarAppearance()
            appearance2.configureWithDefaultBackground()
            appearance2.setBackIndicatorImage(backImageCircle, transitionMaskImage: backImageCircle)

            let appearance3 = UINavigationBarAppearance()
            appearance3.configureWithDefaultBackground()
            appearance3.setBackIndicatorImage(backImageCurve, transitionMaskImage: backImageCurve)

            // 默认导航栏外观
            UINavigationBar.appearance().standardAppearance = appearance1
            // 紧凑型导航栏外观
            UINavigationBar.appearance().compactAppearance = appearance2
            // UIScrollView滚动到导航栏顶部的时候外观
            UINavigationBar.appearance().scrollEdgeAppearance = appearance3
        }
        return true
    }
}

extension UIViewController {
    func screen() -> UIScreen? {
        var parent = self.parent
        var lastParent = parent
        while parent != nil {
            lastParent = parent
            parent = parent?.parent
        }
        if #available(iOS 13.0, *) {
            return lastParent?.view.window?.windowScene?.screen
        } else {
            return nil
        }
    }
}
